package com.tusuo.stds.ui.scene;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tusuo.stds.R;
import com.tusuo.stds.rest.LevelRecordList;
import com.tusuo.stds.rest.ModuleList;
import com.tusuo.stds.util.Utils;

import java.util.List;

/**
 * @author J.Huang
 * @version 1.0
 *
 */
public class ModuleListAdapter extends BaseAdapter {

    private List<ModuleList.Module> dataList;
    private LayoutInflater layoutInflater;
    private Context context;

    public ModuleListAdapter(Context context, List<ModuleList.Module> dataList) {
        this.context = context;
        this.dataList = dataList;
        this.layoutInflater = LayoutInflater.from(context);
    }

    private final class Component {
        RelativeLayout layout;
        TextView tvTitle;
        TextView tvCount;
        ImageView ivLock;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public ModuleList.Module getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Component component = null;
        if (convertView == null) {
            component = new Component();
            // 获得组件，实例化组件
            convertView = layoutInflater.inflate(R.layout.module_list_item, null);

            component.layout = (RelativeLayout) convertView.findViewById(R.id.module_list_item_layout);
            component.tvTitle = (TextView) convertView.findViewById(R.id.module_list_item_tv_title);
            component.tvCount = (TextView) convertView.findViewById(R.id.module_list_item_tv_count);
            component.ivLock = (ImageView) convertView.findViewById(R.id.module_list_item_iv_lock);

            convertView.setTag(component);
        } else {
            component = (Component) convertView.getTag();
        }

        ModuleList.Module module = dataList.get(position);

        // 绑定数据
        component.tvTitle.setText(module.Title);

        // 0: locked, 10: opening
        if (module.LockStatus == 0) {
            component.layout.setBackgroundResource(R.drawable.shape_module_locked);
            component.tvTitle.setTextColor(ContextCompat.getColor(context, R.color.textColorPrimary));
            component.tvCount.setVisibility(View.GONE);
            component.ivLock.setVisibility(View.VISIBLE);
        } else {
            component.layout.setBackgroundResource(R.drawable.shape_module_unlocked);
            component.tvTitle.setTextColor(ContextCompat.getColor(context, R.color.white));
            if (module.TotalCount > 0) {
                component.tvCount.setVisibility(View.VISIBLE);
                component.tvCount.setText(context.getString(R.string.module_count, module.FinishCount, module.TotalCount));
            } else {
                component.tvCount.setVisibility(View.INVISIBLE);
            }
            component.ivLock.setVisibility(View.GONE);
        }

        return convertView;
    }
}
